FlattenGrad

Flatten 算子的反向传播算子,用于将一维梯度按照原始输入张量的形状还原。 该算子不进行数值计算,仅依据原始形状信息对梯度数据进行内存拷贝与重解释,元素顺序保持不变。

\[\text{grad\_input} = \text{reshape}(\text{grad\_output}, \text{origin\_shape})\]
输入:
  • input - 上游梯度(Flatten 输出对应的梯度)地址。

  • origin_shape - 原始输入张量的形状数组。

  • origin_ndim - 原始输入张量的维度数。

  • core_mask - 核掩码(仅适用于共享存储版本)。

输出:
  • output - 还原形状后的梯度数据地址。

支持平台:

FT78NE MT7004

备注

  • FT78NE 支持 fp32

  • MT7004 支持 fp16, fp32

  • 该算子仅进行内存拷贝,不涉及数值运算

  • 输出元素总数等于 origin_shape 各维度之积

共享存储版本:

void fp_flattengrad_s(float *input, float *output, int *origin_shape, int origin_ndim, int core_mask)
void hp_flattengrad_s(half *input, half *output, int *origin_shape, int origin_ndim, int core_mask)

C调用示例:

 1// FT78NE 示例
 2#include <stdio.h>
 3#include <flattengrad.h>
 4
 5int main(int argc, char* argv[]) {
 6    float *grad_flat = (float *)0xA0000000;   // 梯度在 DDR
 7    float *grad_out  = (float *)0xC0000000;
 8
 9    int origin_shape[2] = {32, 128};
10    int origin_ndim = 2;
11    int core_mask = 0xff;
12
13    fp_flattengrad_s(grad_flat, grad_out, origin_shape, origin_ndim, core_mask);
14    return 0;
15}

私有存储版本:

void fp_flattengrad_p(float *input, float *output, int *origin_shape, int origin_ndim)
void hp_flattengrad_p(half *input, half *output, int *origin_shape, int origin_ndim)

C调用示例:

 1// MT7004 示例
 2#include <stdio.h>
 3#include <flattengrad.h>
 4
 5int main(int argc, char* argv[]) {
 6    half *grad_flat = (half *)0x10810000;   // 梯度在 L2
 7    half *grad_out  = (half *)0x10820000;
 8
 9    int origin_shape[3] = {1, 3, 224 * 224};
10    int origin_ndim = 3;
11
12    hp_flattengrad_p(grad_flat, grad_out, origin_shape, origin_ndim);
13    return 0;
14}